package experiment;

/**
 * Created by lenovo on 2016/4/12.
 */
public class ComplexNumber{
    private double real;
    private double imag;

    public ComplexNumber() {
        this.real=0;
        this.imag=0;
    }

    public ComplexNumber(double r,double i){
        this.real = r;
        this.imag = i;
    }

    public double getReal() {
        return real;
    }

    public void setReal(double real) {
        this.real = real;
    }

    public double getImag() {
        return imag;
    }

    public void setImag(double imag) {
        this.imag = imag;
    }

    public ComplexNumber ComplexAdd(ComplexNumber c) {
        double r = this.real + c.real;
        double i = this.imag + c.imag;
        return new ComplexNumber(r,i);
    }

    public ComplexNumber ComplexAdd(double c) {
        double r = this.real + c;
        double i = this.imag;
        return new ComplexNumber(r,i);
    }

    public ComplexNumber ComplexMinus(ComplexNumber c) {
        double r = this.real - c.real;
        double i = this.imag - c.imag;
        return new ComplexNumber(r,i);
    }

    public ComplexNumber ComplexMinus(double c) {
        double r = this.real - c;
        double i = this.imag;
        return new ComplexNumber(r,i);
    }

    public ComplexNumber ComplexMulti(ComplexNumber c) {
        double r = this.real*c.real - this.imag*c.imag;
        double i = this.real*c.imag + this.imag*c.real;
        return new ComplexNumber(r,i);
    }

    public ComplexNumber ComplexMulti(double c) {
        double r = this.real*c;
        double i = this.imag*c;
        return new ComplexNumber(r,i);
    }

    public String toString() {
        if (real==0 && imag==0) return "0";
        if (real==0 && imag!=0) return imag + "i";
        if (imag==0 && real!=0) return real + "";
        if (imag<0) return real + " - " + (-imag) + "i";
        return real + " + " + imag + "i";
    }
}